package com.example.apkmanage.holder;

import java.util.HashMap;

import android.text.TextUtils;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.example.apkmanage.R;

public class MainListViewHolder {
	public static final int KEY_URL = 0;
	public static final int KEY_SPEED = 1;
	public static final int KEY_PROGRESS = 2;
	public static final int KEY_IS_PAUSED = 3;

	public ImageView imageViewIcon;
	public TextView appName;
	public TextView appType;
	public TextView appSize;
	public ImageView imgSplit;
	public ImageButton imgDownLoad;
	public TextView appPriceFlag;
	public LinearLayout progressBarLinearLayout;
	public ProgressBar progressBar;

	private boolean hasInited = false;

	public MainListViewHolder(View convertView) {
		if (convertView != null) {

			imageViewIcon = (ImageView) convertView.findViewById(R.id.image_item_1);
			appName = (TextView) convertView.findViewById(R.id.text_item_1);
			appType = (TextView) convertView.findViewById(R.id.text_item_2);
			appSize = (TextView) convertView.findViewById(R.id.text_item_3);
			imgSplit = (ImageView) convertView.findViewById(R.id.image_item_2);
			imgDownLoad = (ImageButton) convertView.findViewById(R.id.image_item_3);
			appPriceFlag = (TextView) convertView.findViewById(R.id.text_item_4);
			progressBarLinearLayout = (LinearLayout) convertView.findViewById(R.id.progressBarLinearLayout);
			progressBar = (ProgressBar) convertView.findViewById(R.id.progressbar);
			hasInited = true;
		}
	}

	public void setData(HashMap<Integer, String> item) {
		if (hasInited) {
			String progress = item.get(KEY_PROGRESS);
			if (TextUtils.isEmpty(progress)) {
				progressBar.setProgress(0);
			} else {
				progressBar.setProgress(Integer.parseInt(progress));
			}
			if (Boolean.parseBoolean(item.get(KEY_IS_PAUSED))) {
				onPause();
			}
		}
	}

	public void onPause() {
		if (hasInited) {
			imgDownLoad.setVisibility(View.GONE);
			// continueButton.setVisibility(View.VISIBLE);
		}
	}

	public void setData(String url, String speed, String progress) {
		setData(url, speed, progress, false + "");
	}

	public static HashMap<Integer, String> getItemDataMap(String url, String speed, String progress, String isPaused) {
		HashMap<Integer, String> item = new HashMap<Integer, String>();
		item.put(KEY_URL, url);
		item.put(KEY_SPEED, speed);
		item.put(KEY_PROGRESS, progress);
		item.put(KEY_IS_PAUSED, isPaused);
		return item;
	}

	public void setData(String url, String speed, String progress, String isPaused) {
		if (hasInited) {
			HashMap<Integer, String> item = getItemDataMap(url, speed, progress, isPaused);

			//titleText.setText(TAStringUtils.getFileNameFromUrl(item.get(KEY_URL)));
			//speedText.setText(speed);
			if (TextUtils.isEmpty(progress)) {
				progressBar.setProgress(0);
			} else {
				progressBar.setProgress(Integer.parseInt(item.get(KEY_PROGRESS)));
			}

		}
	}

}